﻿using System.Collections.Generic;
using System;
using System.Data;
using System.Data.Common;
namespace WebLog
{
    public sealed class WebLogData
    {

        public List<WebLogEntity> GetListLogs()
        {
            List<WebLogEntity> logs = new List<WebLogEntity>();
            LogDataBase db = new LogDataBase();
            try
            {

                db.Connect();
                db.CreateSql("SELECT * FROM log order by date desc");

                var t = db.ExecuteDataReader();
                foreach (DbDataRecord row in t)
                {
                    WebLogEntity weblog = new WebLogEntity();
                    weblog.id = row.GetValue(0).ToString();
                    weblog.date = row.GetDateTime(1);
                    weblog.level = row.GetString(2);
                    weblog.logger = row.GetString(3);
                    weblog.message = row.GetString(4);
                    logs.Add(weblog);
                }

            }
            catch (Exception e)
            {
                logs = null;
            }

            finally
            {

                db.Disconnect();
                db.ReleaseResources();
            }

            return logs;

        }
    }
}
